Passed
Push — master ( 75c06e...4206c8 )
by Night
01:08
created

stringFuncs.getWordAt   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 23
nc 18
nop 2
dl 0
loc 34
rs 8.3946
c 0
b 0
f 0
1
2
UB.wordSeperators = [" ", ".", ",", ";", "[", "]", "{", "}", "(", ")"];
0 ignored issues
show
Bug introduced by
The variable UB seems to be never declared. If this is a global, consider adding a /** global: UB */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
3
4
5
var stringFuncs = {
6
	
7
	/** advanced: splits by any operator or number char, space & newline.
8
	 * simple: splits by newline & space.
9
	 * always returns an array of non-blank, trimmed words. */
10
	splitWords: function(advanced = true){
11
		var text = this;
12
13
		if (advanced){
14
			
15
			var results = [];
16
			
17
			// find start of word
18
			for (var c = 0, cl = text.length;c<cl;c++){
19
				var cc = text.charCodeAt(c);
20
				var start = c;
21
				
22
				if (Chars.IsLetter(cc)) {
0 ignored issues
show
Bug introduced by
The variable Chars seems to be never declared. If this is a global, consider adding a /** global: Chars */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
23
					
24
					// find end of word
25
					for (var e = c + 1;e<cl;e++){
26
						var ec = text.charCodeAt(e);
27
						var isLastChar = (e == (cl - 1));
28
						
29
						if ((!Chars.IsLetter(ec) && ec != charCodes.Space) || isLastChar){
0 ignored issues
show
Bug introduced by
The variable charCodes seems to be never declared. If this is a global, consider adding a /** global: charCodes */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
30
							
31
							// skip if "letter [dash] letter"
32
							if (!isLastChar && ec == charCodes.Minus && Chars.IsLetter(text.charCodeAt(e + 1))){
33
								continue;
34
							}
35
							
36
							// add word if something found
37
							var end = isLastChar?e:e-1;
38
							if (end > start) {
39
								
40
								var term = text.getRange(start, end).trim();
41
								if (S.Exists(term)){
0 ignored issues
show
Bug introduced by
The variable S seems to be never declared. If this is a global, consider adding a /** global: S */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
42
									
43
									if (splitBySpaceAlso) {
0 ignored issues
show
Best Practice introduced by
If you intend to check if the variable splitBySpaceAlso is declared in the current environment, consider using typeof splitBySpaceAlso === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
44
										A.Add(results, S.SplitWords(term));
0 ignored issues
show
Bug introduced by
The variable A seems to be never declared. If this is a global, consider adding a /** global: A */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
45
									} else {
46
										results.push(term);
47
									}
48
								}
49
							}
50
							
51
							// continue at ending
52
							c = e;
1 ignored issue
show
Complexity Coding Style introduced by
You seem to be assigning a new value to the loop variable c here. Please check if this was indeed your intention. Even if it was, consider using another kind of loop instead.
Loading history...
53
							break;
54
						}
55
					}
56
				}
57
			}
58
			return results;
59
60
		}else{
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
61
62
			// SPLIT BY NEWLINE & SPACES
63
			var results = [];
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable results already seems to be declared on line 15. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
64
			var lines = text.splitLines(true, true);
65
			for (var s = 0, sl = lines.length; s < sl; s++) {
66
				var line = lines[s];
67
				var results = line.split(" ");
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable results already seems to be declared on line 15. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
68
				for (var w = 0, wl = results.length; w < wl; w++) {
69
					var word = results[w];
70
					if (word.length > 0) {
71
						results.push(word);
72
					}
73
				}
74
			}
75
			return results;
76
			
77
		}
78
	},
79
	
80
	joinWords: function(){
81
		var words = this;
82
		return words.join(" ");
83
	},
84
	
85
	/** Count words in a string */
86
	wordCount: function(){
87
		var text = this;
88
		return text.match(new RegExp("\\b\\w+\\b", "g")).length;
89
	},
90
	none:null
91
};
92
93
// register funcs
94
UB.registerFuncs(String.prototype, stringFuncs);